package com.zengqingfa.examples.mybatisplus.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.reflect.Method;
import java.util.Properties;

/**
 *
 * @fileName: TestInterceptor
 * @author: zengqf3
 * @date: 2021-4-25 16:28
 * @description:
 */

/**
 * @Intercepts：标识该类是一个拦截器；
 * @Signature：指明自定义拦截器需要拦截哪一个类型，哪一个方法；
 * 2.1 type：对应四种类型中的一种；
 * 2.2 method：对应接口中的哪个方法；
 * 2.3 args：对应方法的参数类型class
 */
@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
        RowBounds.class, ResultHandler.class})})
public class TestInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object target = invocation.getTarget(); //被代理对象
        Method method = invocation.getMethod(); //代理方法
        Object[] args = invocation.getArgs(); //方法参数
        // do something ...... 方法拦截前执行代码块
        Object result = invocation.proceed();
        // do something .......方法拦截后执行代码块
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
